(function(root){
    //默认参数
    var defaults = {
        duration: 1000,
        selector: '.page',
        currentIndex: -1,
        afterChange: function(data){console.log(data)}
    }
    //内部函数，合并对象
    var extend = function(){
        var results = {},
            i = 0,
            deep = false,
            length = arguments.length;
        if(typeof arguments[0] === "boolean"){
            i = 1;
            deep = arguments[0];
        }
        while(i<length){
            loop(arguments[i]);
            i++;
        }
        function loop(obj){
            for(var k in obj){
                if(deep && Object.prototype.toString.call(obj[k]) === '[object Object]'){
                    results[k] = extend(true, obj[k]);
                }else{
                    results[k] = obj[k];
                }
            }
        }
        return results;
    }

    //封装FullPage类
    function FullPage(ele, options){
        this.ele = ele;
        this.settings = extend(options, defaults);
        this.init();
    }

    FullPage.prototype = {
        constructor: FullPage,

        init: function(){
            this.initDom();
            this.initEvent();
        },

        initDom: function(){
            this.movingFlag = false;
            this.parant = this.ele.parentNode;
            this.H = this.parant.offsetHeight;
            this.pagers = this.ele.querySelectorAll(this.settings.selector);
            this.len = this.pagers.length;
            for(var i=0; i<this.len; i++){
                this.pagers[i].style.height = this.H + 'px';
            }
            this.moveTo(this.settings.currentIndex, 0);
            this.ele.classList.add('anim');
        },

        initEvent: function(){
            var self = this;
            this.ele.addEventListener('touchstart', function(e){
                if(self.movingFlag) return;
                this.startY = e.targetTouches[0].pageY;
            })
            this.ele.addEventListener('touchend', function(e){
                if(self.movingFlag) return;
                var endY = e.changedTouches[0].pageY;
                var distance = endY - this.startY;
                if(Math.abs(distance)/self.H < 0.1) return;
                var next = distance > 0? self.fix(self.settings.currentIndex, 'prev') : self.fix(self.settings.currentIndex, 'next');
                if(next === self.settings.currentIndex) return;
                self.moveTo(self.settings.currentIndex, next);
            })
        },

        fix: function(index, type){
            if(type === 'prev'){
                return index <= 0? 0 : --index;
            }else if(type === 'next'){
                return index < this.len-1? ++index : this.len-1;
            }
        },

        moveTo: function(cur, next){
            this.movingFlag = true;
            var offsetDistance = -next*this.H+'px';
            this.ele.style.transform = 'translate3d(0px, '+ offsetDistance +', 0px)';
            this.ele.style.webkitTransform = 'translate3d(0px, '+ offsetDistance +', 0px)';

            setTimeout(function(){
                this.settings.afterChange({
                    prev: cur,
                    cur: next
                });
                this.settings.currentIndex = next;
                this.movingFlag = false;
            }.bind(this), this.ele.classList.contains('anim')? this.settings.duration: 0);
        }
    }
    //FullPage导出给全局引用
    root.FullPage = function(ele, options){
        new FullPage(ele, options);
    }
}(window))